package com.lv.suyiyong.hfrbusines.ui

import android.text.Editable
import android.text.TextWatcher
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.lv.common.base.BaseActivity
import com.lv.common.ktx.setSafeListener
import com.lv.common.network.model.DataResult
import com.lv.common.utils.ARoutUtils
import com.lv.common.utils.StatusBarUtil
import com.lv.common.utils.showToast
import com.lv.service.model.ServiceViewModel
import com.lv.service.network.BaseYgxRsp
import com.lv.service.network.onBizError
import com.lv.service.network.onBizOK
import com.lv.service.network.onSuccess
import com.lv.suyiyong.hfrbusines.R
import com.lv.suyiyong.hfrbusines.databinding.ActivityFeedBackBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 * 作者： ygx
 * 创建日期：2023/5/6 10:33 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：意见反馈
 */
@Route(path = ARoutUtils.FeedBackActivity)
class FeedBackActivity : BaseActivity<ActivityFeedBackBinding>() {
    private val viewModel: ServiceViewModel by viewModel()
    private val myScope: CoroutineScope = CoroutineScope(SupervisorJob())

    override fun getLayoutRes() = R.layout.activity_feed_back
    override fun initView() {
        super.initView()
        initStatusBar(this, true, true)
        ARouter.getInstance().inject(this)
        mBinding.apply {
            StatusBarUtil.setHeightAndPadding(this@FeedBackActivity, titleBar)
            titleBar.onClickBackIcon { finish() }
            titleBar.setTitle("意见反馈")
            tvSubmit.alpha = 0.2f
            tvSubmit.isEnabled = false
            tvSubmit.setSafeListener {
                val content = etContent.text.toString().trim()
                if (content.isEmpty()) {
                    showToast("请输入文字")
                    return@setSafeListener
                }
                feedback(content)
            }
        }
    }


    override fun initConfig() {
        super.initConfig()
        mBinding.etContent.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (s != null && s.isNotEmpty()) {
                    mBinding.tvSubmit.alpha = 1.0f
                    mBinding.tvSubmit.isEnabled = true

                } else {
                    mBinding.tvSubmit.alpha = 0.2f
                    mBinding.tvSubmit.isEnabled = false
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }
        })

    }


    override fun initData() {
        super.initData()

    }

    private fun feedback(content: String) {
        startLoading("提交中……")
        lifecycleScope.launchWhenCreated {
            val result: DataResult<BaseYgxRsp> =
                myScope.async { viewModel.feedback(content) }.await()
            parseResult(result)
        }
    }

    private fun parseResult(data: DataResult<BaseYgxRsp>) {
        loadFinished()
        data.onSuccess {
            onBizOK<Any> { code, data, message, result ->
                finish()

            }
            onBizError<Any> { code, message, data ->
                showToast(message)
            }
        }
    }

}